﻿#include "StdAfx.h"
#include "DBProc.h"
#include <stdlib.h>

#import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll>  \
	rename( "EOF", "AdoNSEOF" )
_bstr_t strConnect = 
	"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= Word.accdb;";

char* Add(char* src,int len,char *dic = "EN")
{
	int i;
	int k = 0;
	char *kq = new char[len];
	char*temp = new char[len];
	long t = atol(src);
	t++;
	ltoa(t,temp,10);
	strcpy(kq,dic);
	for(i = 2; i < len-strlen(temp);i++)
	{
		kq[i] = '0';
	}
	for(;i < len;i++)
		kq[i] = temp[k++];
	kq[i] = 0;
	return kq;
}

DBProc::DBProc(void)
{
}


DBProc::~DBProc(void)
{
}

_bstr_t DBProc::GetWordOfDic(_bstr_t word,_bstr_t dicname,_bstr_t dictype)
{
	_bstr_t MaTu = "";
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return "";
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");

		//QUERY:Get wordcode of dic
		_bstr_t query = "SELECT Tu.MaTu From Tu,TuDien,TuCuaTuDien WHERE CachViet = \"";
		query += word;
		query += "\" and Tu.MaTu = TuCuaTuDien.MaTu and TuCuaTuDien.MaTuDien = TuDien.MaTuDien and TenTuDien = \"";
		query += dicname;
		query += "\" and LoaiTuDien = \"";
		query += dictype;
		query += "\"";

		hr = pRs->Open(query, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						MaTu = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
			}
			else 
			{
				pRs->Close();
				pConn->Close();
				return "";
			}

		}
		else
		{
			pRs->Close();
			pConn->Close();
			return "";
		}
		pRs->Close();
		pConn->Close();
	}
	catch(_com_error *e)
	{
		return "";
	}
	CoUninitialize();
	return MaTu;
}

_bstr_t DBProc::GetMeanOfWord(_bstr_t word,_bstr_t dicname,_bstr_t dictype)
{
	_bstr_t mean;
	LPTSTR type;
	LPTSTR example;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return "Error : Init Com....";
	}
	try
	{
		_bstr_t MaTu = GetWordOfDic(word,dicname,dictype);
		if(MaTu.operator==(""))
		{
			return "Not Found";
		}
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		if(SUCCEEDED(hr))//Success->query mean of word
		{
			//Prepare query
			_bstr_t temp = "\"";
			temp += MaTu;
			temp += "\"";
			_bstr_t meanquery = "SELECT CachPhatAm,TuLoai,Nghia,ViDu FROM YNghia,Tu,NghiaTu WHERE Tu.MaTu = NghiaTu.MaTu and NghiaTu.MaYNghia = YNghia.MaYNghia and Tu.MaTu = ";
			meanquery += temp;
			meanquery += "ORDER BY YNghia.TuLoai ASC";
			//Execute query and save in recordset object
			ADODB::_RecordsetPtr pRs("ADODB.Recordset");//Create record set to save result of query
			//Execute
			hr = pRs->Open(meanquery, 
				_variant_t((IDispatch*)pConn,true), 
				ADODB::adOpenUnspecified,
				ADODB::adLockUnspecified, 
				ADODB::adCmdText);

			if(SUCCEEDED(hr))//success query
			{
				ADODB::Fields *pFields = NULL;//Init field to get info
				hr = pRs->get_Fields(&pFields);
				int count = pFields->GetCount();
				int n = 0;
				if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
				{
					while (!pRs->AdoNSEOF)
					{
						for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
						{
							TCHAR current[20];
							TCHAR last[20];
							if(nIndex == 0 && n != 0)
								continue;
							_variant_t t = pFields->GetItem(nIndex)->GetValue();
							if(t.vt == VT_NULL)
								continue;
							temp = _bstr_t(t);
							if(nIndex == 1)
							{
								if(temp.operator==("n"))
									type = L"Danh từ";
								else if(temp.operator==("v"))
									type = L"Động từ";
								else if(temp.operator==("pp"))
									type = L"Giới từ";
								else if(temp.operator==("aj"))
									type = L"Tính từ";
								else if(temp.operator==("av"))
									type = L"Trạng từ";
								else if(temp.operator==("vt"))
									type = L"Ngoại động từ";
								else if(temp.operator== ("vi"))
									type = L"Nội động từ";
								else if(temp.operator== ("ab"))
									type = L"Viết tắt";
								else
									type = L"Không xác định";
								temp = _bstr_t(type);
								wcscpy(current,type);
								
							}
							if(nIndex==0)
							{
								mean += temp;
								mean += "\n";
							}
							if(nIndex==1)
							{
								
								mean += "\n";
								if(wcscmp(last,current)!=0)
								{
									mean += temp;
									mean += "\n";
								}
								wcscpy(last,current);
							}
							if(nIndex==2)
							{	
								
								example = temp;
								example = wcstok(temp,L"\n");

								mean+="       - ";
								mean += example;

								do
								{
									
									
									example = wcstok(NULL,L"\n");

									if(example==NULL)
									{
										mean += "\n";
										break;
									}
									mean += "\n";
									mean+="       - ";
									mean += example;
									

								}while(example!=NULL);
							}
							if(nIndex==3)
							{
								int nLine = 0;
								example = temp;
								example = wcstok(temp,L"\n");
								
								mean+="            ";
								mean += example;
								
								do
								{
									nLine++;
									
 									if (nLine%2==0)
										mean += "\n";
									example = wcstok(NULL,L"\n");
									if(example==NULL)
										break;
									
									mean+="            ";
									mean += example;
									mean += "\n";
									
								}while(example!=NULL);
								mean += "\n";
							}
							
							temp = "";
						}
						n++;
						pRs->MoveNext();
					}
				}

				pRs->Close();
				pConn->Close();
			}
 		}
	}
	catch(_com_error& e)
	{
		//cout << "_COM_error: "<< e.Description() << endl;
		return "Error....";
	}
	CoUninitialize();
	return mean;
}


bool DBProc::AddWord(_bstr_t pronoun,_bstr_t word,_bstr_t dic,
	_bstr_t mean,_bstr_t type,_bstr_t exam,int flag)
{
	_bstr_t MaTu,MaNghia;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return false;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		_bstr_t newwordcode,newmeancode;
		
		//Prepare query
		//============================================================================================
		//QUERY: get number of word
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");//Create record set to save result of query
		_bstr_t getcount = "SELECT MaTu,MaYNghia FROM NghiaTu WHERE MaTu >= ALL(SELECT MaTu From NghiaTu)";
		pRs->Open(getcount,
			_variant_t((IDispatch*)pConn,true),
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText
			);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && count > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < count; nIndex++)
					{
						if(nIndex %2 == 0)
							MaTu = _bstr_t(pFields->GetItem(nIndex)->GetValue());
						else
							MaNghia = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)//no word found in table
				{
					MaTu = "EN0000000001";
					MaNghia = "EN000000000001";
				}
				else//Create new code of word and mean
				{
					char *code = (char*)MaTu;
					code[0] =code[1] =  '0';
					char *newcode = Add(code,strlen(code));
					newwordcode = _bstr_t(newcode);

					char *m = (char*)MaNghia;
					m[0] = m[1] = '0';
					newcode = Add(m,strlen(m));
					newmeancode = _bstr_t(newcode);
				}
			}
			else
				return false;//Query fail
		}
		else return false;
		pRs->Close();
		//============================================================================================
		//QUERY add word: add row in Tu table
		if(flag != 1)
		{
			_bstr_t addwordquery = "INSERT INTO Tu(MaTu,CachPhatAm,CachViet) VALUES(";
			addwordquery += "\"";
			addwordquery += newwordcode;
			addwordquery += "\",";

			addwordquery += "\"";
			addwordquery += pronoun;
			addwordquery += "\",";

			addwordquery += "\"";
			addwordquery += word;
			addwordquery += "\")";

			hr = pConn->Execute(addwordquery,NULL,ADODB::adExecuteNoRecords);
			if(FAILED(hr))
			{
				//pRs->Close();
				pConn->Close();
				return false;
			}
		}
		//============================================================================================
		//QUERY add mean: add row in YNghia table
		_bstr_t addmeanquery = "INSERT INTO YNghia(MaYNghia,Nghia,ViDu,TuLoai) VALUES(";
		addmeanquery += "\"";
		addmeanquery += newmeancode;
		addmeanquery += "\",";

		addmeanquery += "\"";
		addmeanquery += mean;
		addmeanquery += "\",";

		addmeanquery += "\"";
		addmeanquery += exam;
		addmeanquery += "\",";

		addmeanquery += "\"";
		addmeanquery += type;
		addmeanquery += "\")";

		hr = pConn->Execute(addmeanquery,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			//pRs->Close();
			pConn->Close();
			return false;
		}
		//============================================================================================
		//QUERY: add row reference in NghiaTu table
		_bstr_t addref = "INSERT INTO NghiaTu(MaYNghia,MaTu) VALUES(";
		addref += "\"";
		addref += newmeancode;
		addref += "\",";

		addref += "\"";
		if(flag != 1)
			addref += newwordcode;
		else
			addref += MaTu;
		addref += "\")";

		hr = pConn->Execute(addref,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			//pRs->Close();
			pConn->Close();
			return false;
		}
		//=============================================================================================
		//QUERY:Update number of word
		if(flag != 1)
		{
			_bstr_t updatecount = "UPDATE TuDien SET SoLuongTu = SoLuongTu + 1";
			hr = pConn->Execute(updatecount,NULL,ADODB::adExecuteNoRecords);
			if(FAILED(hr))
			{
				//pRs->Close();
				pConn->Close();
				return false;
			}
		}
		pConn->Close();
	}
	catch(_com_error* e)
	{
		return false;
	}
	CoUninitialize();
	return true;
}


bool DBProc::DeleteWord(_bstr_t word)
{
	_bstr_t MaTu,MaNghia;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return false;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		//Prepare query
		//=========================================================================================
		//QUERY: Find word code
		_bstr_t findquery = "SELECT Tu.MaTu,NghiaTu.MaYNghia FROM Tu,NghiaTu WHERE Tu.MaTu = NghiaTu.MaTu and Tu.CachViet = ";
		findquery += "\"";
		findquery += word;
		findquery += "\"";

		ADODB::_RecordsetPtr pRs("ADODB.Recordset");//Create record set to save result of query
		//Execute
		hr = pRs->Open(findquery, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						if(nIndex %2 == 0)
							MaTu = _bstr_t(pFields->GetItem(nIndex)->GetValue());
						else
							MaNghia = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs->Close();
					pConn->Close();
					return false;//no word found in table
				}
			}
			else
			{
				pRs->Close();
				pConn->Close();
				return false;//Query fail
			}
		}
		else
		{
			pRs->Close();
			pConn->Close();
			return false;
		}
		//=========================================================================================
		//QUERY:Delete mean
		_bstr_t delmeanquery = "DELETE FROM YNghia WHERE MaYNghia = ";
		delmeanquery += "\"";
		delmeanquery += MaNghia;
		delmeanquery +="\"";
		hr = pConn->Execute(delmeanquery,NULL,ADODB::adExecuteNoRecords);

		if(FAILED(hr))
		{	
			pRs->Close();
			pConn->Close();
			return false;
		}
		//==========================================================================================
		//QUERY:Del mean of word
		_bstr_t delref = "DELETE FROM NghiaTu WHERE MaTu = ";
		delref += "\"";
		delref += MaTu;
		delref += "\"";
		hr = pConn->Execute(delref,NULL,ADODB::adExecuteNoRecords);

		if(FAILED(hr))
		{	
			pRs->Close();
			pConn->Close();
			return false;
		}

		//===========================================================================================
		//QUERY :Del word
		_bstr_t delwordquery = "DELETE FROM Tu WHERE MaTu = ";
		delwordquery += "\"";
		delwordquery += MaTu;
		delwordquery +="\"";
		hr = pConn->Execute(delwordquery,NULL,ADODB::adExecuteNoRecords);

		if(FAILED(hr))
		{
			pRs->Close();
			pConn->Close();
			return false;
		}

		//=============================================================================================
		//QUERY:Update number of word
		_bstr_t updatecount = "UPDATE TuDien SET SoLuongTu = SoLuongTu - 1";
		hr = pConn->Execute(updatecount,NULL,ADODB::adExecuteNoRecords);

		if(FAILED(hr))
		{	
			pRs->Close();
			pConn->Close();
			return false;
		}
	}
	catch(_com_error* e)
	{
		return false;
	}
	CoUninitialize();
	return true;
}


_bstr_t DBProc::CountWord(_bstr_t dicName, _bstr_t dicType)
{
	_bstr_t SoTu;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return "Error";
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		//=========================================================================================
		//QUERY:Get number of dic 
		_bstr_t strsql = "SELECT SoLuongTu FROM TuDien WHERE TuDien.LoaiTuDien = ";
		strsql += "\'";
		strsql += dicType;
		strsql += "\'";
		strsql += " and TuDien.TenTuDien = ";
		strsql += "\'";
		strsql += dicName;
		strsql += "\'";

		//Execute
		hr = pRs->Open(strsql, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		//Get info
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						SoTu = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs->Close();
					pConn->Close();
					return "Error";//no word found in table
				}
			}
			else
			{
				pRs->Close();
				pConn->Close();
				return "Error";//no word found in table
			}
		}
	}
	catch(_com_error* e)
	{
		return "Error";
	}
	CoUninitialize();
	return SoTu;
}


_bstr_t DBProc::ListWord(_bstr_t dicName, _bstr_t dicType)
{
	_bstr_t list = "";
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return list;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		//=========================================================================================
		//QUERY:Get word 
		_bstr_t strsql = "SELECT t.CachViet From Tu t , TuCuaTuDien tctd, TuDien td where t.MaTu = tctd.MaTu and td.MaTuDien = tctd.MaTuDien and td.LoaiTuDien = ";
		strsql+="\'";
		strsql+=dicType;
		strsql+="\'";
		strsql+="and td.TenTuDien = ";
		strsql+="\'";
		strsql+=dicName;
		strsql+="\'";

		//Execute
		hr = pRs->Open(strsql, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		//Get info
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{

						list += _bstr_t(pFields->GetItem(nIndex)->GetValue());
						list += "\n";
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs->Close();
					pConn->Close();
					return list;//no word found in table
				}
			}
			else
			{
				pRs->Close();
				pConn->Close();
				return list;//no word found in table
			}
		}
	}
	catch(_com_error* e)
	{
		return list;
	}
	CoUninitialize();
	return list;
}

_bstr_t DBProc::ListDict()
{
	_bstr_t list = "";
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return list;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		//=========================================================================================
		//QUERY:Get word 
		_bstr_t strsql = "SELECT LoaiTuDien,TenTuDien FROM TuDien";

		//Execute
		hr = pRs->Open(strsql, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		//Get info
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						if(nIndex == 0)
						{
							list += _bstr_t(pFields->GetItem(nIndex)->GetValue());
							list += "(";
						}
						else if(nIndex==1)
						{
							list += _bstr_t(pFields->GetItem(nIndex)->GetValue());
							list += ")\n";
						}
						
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs->Close();
					pConn->Close();
					return list;//no word found in table
				}
			}
			else
			{
				pRs->Close();
				pConn->Close();
				return list;//no word found in table
			}
		}
	}
	catch(_com_error* e)
	{
		return list;
	}
	CoUninitialize();
	return list;
}
bool DBProc::AddDic(_bstr_t type,_bstr_t name)
{
	_bstr_t code,temp ;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return false;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		//=========================================================================================
		//Query :test same name//fix lai cho toi uu
		_bstr_t testquery = "SELECT LoaiTuDien,TenTuDien From TuDien";
		pRs->Open(testquery, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			_bstr_t dictype,dicname;
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						if(nIndex % 2 == 0)
							dictype = _bstr_t(pFields->GetItem(nIndex)->GetValue());
						else
							dicname = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					if(dictype == type && dicname == name)
					{
						pRs->Close();
						pConn->Close();
						return false;
					}
					pRs->MoveNext();
				}
			}
			else
			{
				pRs->Close();
				pConn->Close();
				return false;
			}
		}
		//==================================================================================================
		//QUERY:Get dic code
		_bstr_t query = "SELECT MaTuDien From TuDien Where MaTuDien >= ALL(SELECT MaTuDien FROM TuDien)";

		ADODB::_RecordsetPtr pRs1("ADODB.Recordset");
		//Execute
		pRs1->Open(query, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs1->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs1->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						temp = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs1->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs1->Close();
					pConn->Close();
					code = "01";//no word found in table
				}
			}
			else
			{
				pRs1->Close();
				pConn->Close();
				return false;
			}
			//=================================================================================================
			//Get new code
			char *i = (char*)temp;
			int dic = atoi(i);
			_itoa(dic+1,i,10);
			code = _bstr_t(i);

			//Add new record to table
			//Query
			_bstr_t addquery = "INSERT INTO TuDien(MaTuDien,LoaiTuDien,TenTuDien,SoLuongTu) VALUES(";
			addquery += "\"0";
			addquery += code;
			addquery += "\",";
			addquery += "\"";
			addquery += type;
			addquery += "\",";
			addquery += "\"";
			addquery += name;
			addquery += "\",";
			addquery += "0)";
			//Execute
			hr = pConn->Execute(addquery,NULL,ADODB::adExecuteNoRecords);
			if(FAILED(hr))
			{
				pConn->Close();
				pRs->Close();
				return false;
			}
		}
		pRs->Close();
		pRs1->Close();
		pConn->Close();
	}
	catch(_com_error* e)
	{
		return false;
	}
	CoUninitialize();
	return true;
}

bool DBProc::IsExist(_bstr_t param1,int flag,_bstr_t param2,_bstr_t param3,_bstr_t param4)
{
	_bstr_t testquery,testmeanquery;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return false;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		if(flag != 1)
		{
			//Prepare query
			if(flag == 0)//Test word
			{
				_bstr_t MaTu = GetWordOfDic(param1,param3,param4);
				if(MaTu.operator!= (""))
				{
					pRs->Close();
					pConn->Close();
					return false;
				}
			}
			else
				if(flag == 2)//Test type of dic
				{
					testquery = "SELECT MaTuDien FROM TuDien WHERE TenTuDien = \"";
					testquery += param2;
					testquery += "\" and LoaiTuDien = \"";
					testquery += param1 ; 
					testquery += "\"";
					hr = pRs->Open(testquery, 
						_variant_t((IDispatch*)pConn,true), 
						ADODB::adOpenUnspecified,
						ADODB::adLockUnspecified, 
						ADODB::adCmdText);
					int nRow = 0;
					if(SUCCEEDED(hr))
					{
						ADODB::Fields *pFields = NULL;//Init field to get info
						hr = pRs->get_Fields(&pFields);
						int count = pFields->GetCount();
						if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
						{
							while (!pRs->AdoNSEOF)
							{
								nRow ++;
								pRs->MoveNext();
							}
						}
						pRs->Close();
						if(nRow == 0)
							return true;
						else return false;
					}
				}
		}
		//Test mean
		else
		{
			_bstr_t MaTu = GetWordOfDic(param1,param3,param4);
			if(MaTu.operator==(""))
				return false;
			_bstr_t MaNghia;
			ADODB::_RecordsetPtr pRs1("ADODB.Recordset");
			testmeanquery = "SELECT MaYNghia From YNghia WHERE Nghia =\"";
			testmeanquery += param2 ; 
			testmeanquery += "\"";
			hr = pRs1->Open(testmeanquery, 
				_variant_t((IDispatch*)pConn,true), 
				ADODB::adOpenUnspecified,
				ADODB::adLockUnspecified, 
				ADODB::adCmdText);
			int nRow = 0;
			if(SUCCEEDED(hr))
			{
				ADODB::Fields *pFields = NULL;//Init field to get info
				hr = pRs1->get_Fields(&pFields);
				int count = pFields->GetCount();
				if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
				{
					while (!pRs1->AdoNSEOF)
					{
						for (long nIndex=0; nIndex < count; nIndex++)
						{
							MaNghia =  _bstr_t(pFields->GetItem(nIndex)->GetValue());
						}
						nRow ++;
						pRs1->MoveNext();
					}
				}
				pRs1->Close();
				if(nRow == 0)//Chua ton tai nghia
				{
					pConn->Close();
					return true;
				}
				else//Da co nghia=>Xet xem co phai nghia cua tu do hay khong
				{
					ADODB::_RecordsetPtr pRs2("ADODB.Recordset");
					_bstr_t test = "SELECT MaTu From NghiaTu WHERE MaTu = \"";
					test += MaTu;
					test += "\" and MaYNghia = \"";
					test += MaNghia;
					test += "\"";
					hr = pRs2->Open(test, 
						_variant_t((IDispatch*)pConn,true), 
						ADODB::adOpenUnspecified,
						ADODB::adLockUnspecified, 
						ADODB::adCmdText);
					nRow = 0;
					if(SUCCEEDED(hr))
					{
						ADODB::Fields *pFields = NULL;//Init field to get info
						hr = pRs2->get_Fields(&pFields);
						int count = pFields->GetCount();
						if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
						{
							while (!pRs2->AdoNSEOF)
							{
								nRow ++;
								pRs2->MoveNext();
							}
						}
						pRs2->Close();
						if(nRow == 0)
						{
							pConn->Close();
							return true;
						}
						else
						{
							pConn->Close();
							return false;
						}
					}
					
				}
			}
		}
		pRs->Close();
		pConn->Close();
	}
	catch(_com_error *e)
	{
		return false;
	}
	CoUninitialize();
	return true;
}


int DBProc::AddWordOnly(_bstr_t pronoun,_bstr_t word,_bstr_t namedic,_bstr_t typedic)
{
	_bstr_t newcode,code,dic;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return -1;
	}
	try
	{
		bool t = IsExist(word,0);
		if(t == false)
		{
			return 0;//da ton tai
		}
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		ADODB::_RecordsetPtr pRs1("ADODB.Recordset");
		//Query
		_bstr_t query = "SELECT MaTu From Tu WHERE MaTu >= ALL(SELECT MaTu From Tu)";
		hr = pRs->Open(query, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						code = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					newcode = "EN0000000001";//no word found in table
				}
				else
				{
					char *codetemp = (char*)code;
					codetemp[0] =codetemp[1] =  '0';
					char *wordcode = Add(codetemp,strlen(codetemp));
					newcode = _bstr_t(wordcode);
				}
			}
			else 
			{
				pRs->Close();
				pConn->Close();
				return -2;
			}

		}
		else
		{
			pRs->Close();
			pConn->Close();
			return -2;
		}
		pRs->Close();

		//Lay ma tu dien
		query = "SELECT MaTuDien From TuDien WHERE TenTuDien = \"";
		query += namedic;
		query += "\" and LoaiTuDien = \"";
		query += typedic;
		query += "\"";
		hr = pRs1->Open(query, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs1->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs1->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						dic = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs1->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					pRs1->Close();
					pConn->Close();
					return -3; //Khong tim thay tu dien
				}
			}
			else
			{
				pRs1->Close();
				pConn->Close();
				return -2;//Query khong thanh cong
			}
		}
		else 
		{
			pRs1->Close();
			pConn->Close();
			return -2;
		}
		pRs1->Close();

		//===========================================================================================
		//QUERY addword to Tu table
		_bstr_t addwordquery = "INSERT INTO Tu(MaTu,CachPhatAm,CachViet) VALUES(\"";
		addwordquery += newcode;
		addwordquery += "\",\"";

		addwordquery += pronoun;
		addwordquery += "\",\"";

		addwordquery += word;
		addwordquery += "\")";

		hr = pConn->Execute(addwordquery,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			//pRs->Close();
			pConn->Close();
			return -2;
		}
		//=============================================================================================
		//QUERY:add word to TuCuaTuDien table
		_bstr_t addwordofdic = "INSERT INTO TuCuaTuDien(MaTu,MaTuDien) VALUES(\"";
		addwordofdic += newcode;
		addwordofdic += "\",\"";
		addwordofdic += dic;
		addwordofdic += "\")";
		hr = pConn->Execute(addwordofdic,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			pConn->Close();
			return -2;
		}

		_bstr_t updatecount = "UPDATE TuDien SET SoLuongTu = SoLuongTu + 1 WHERE MaTuDien = \"";
		updatecount += dic;
		updatecount += "\"";
		hr = pConn->Execute(updatecount,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			//pRs->Close();
			pConn->Close();
			return false;
		}

		pConn->Close();
	}
	catch(_com_error *e)
	{
		return -1;
	}
	CoUninitialize();
	return 1;
}

int DBProc::AddMeanOnly(_bstr_t word,_bstr_t mean,_bstr_t type,_bstr_t exam,_bstr_t dicname,_bstr_t dictype)
{
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return -1;
	}
	try
	{
		bool t = IsExist(word,1,mean,dicname,dictype);
		if(t == false)
		{
			return 0;
		}
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		_bstr_t wordcode,newmeancode;

		//Prepare query
		//============================================================================================
		//QUERY: get number of word
		wordcode = GetWordOfDic(word,dicname,dictype);
		//===============================================================================================
		//QUERY :get new code of mean
		ADODB::_RecordsetPtr pRs1("ADODB.Recordset");
		_bstr_t newcodequery = "SELECT MaYNghia From YNghia WHERE MaYNghia >= ALL(SELECT MaYNghia From YNghia)";
		hr = pRs1->Open(newcodequery, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		if(SUCCEEDED(hr))
		{
			_bstr_t code;
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs1->get_Fields(&pFields);
			int count = pFields->GetCount();
			int nRow = 0;
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs1->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
					{
						code = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs1->MoveNext();
					nRow++;
				}
				if(nRow == 0)
				{
					newmeancode = "EN000000000001";//no mean found in table
				}
				else
				{
					char *codetemp = (char*)code;
					codetemp[0] =codetemp[1] =  '0';
					char *meancode = Add(codetemp,strlen(codetemp));
					newmeancode = _bstr_t(meancode);
				}
			}
		}
		else
		{
			pRs1->Close();
			pConn->Close();
			return -2;
		}
		pRs1->Close();
		//=========================================================================================
		//QUERY:add to mean table
		_bstr_t addmeanquery = "INSERT INTO YNghia(MaYNghia,Nghia,ViDu,TuLoai) VALUES(\"";
		addmeanquery += newmeancode;
		addmeanquery += "\",\"";

		addmeanquery += mean;
		addmeanquery += "\",\"";

		addmeanquery += exam;
		addmeanquery += "\",\"";

		addmeanquery += type;
		addmeanquery += "\")";

		hr = pConn->Execute(addmeanquery,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			pConn->Close();
			return false;
		}

		//===================================================================================================
		//QUERY :Add to NghiaTu table
		_bstr_t addquery = "INSERT INTO NghiaTu(MaYNghia,MaTu) VALUES(\"";
		addquery += newmeancode;
		addquery += "\",\"";
		addquery += wordcode;
		addquery += "\")";

		hr = pConn->Execute(addquery,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			pConn->Close();
			return false;
		}
		pConn->Close();
	}
	catch(_com_error *e)
	{
		return -1;
	}
	CoUninitialize();
	return 1;
}

int DBProc::AddRefWord(_bstr_t word,_bstr_t refword,_bstr_t type)
{
	char* a = (char*)type;
	if(strlen(a) > 2)
	{
		return -2;//tham so khong dung
	}
	_bstr_t MaTu,MaTuLienQuan;
	// Initialize COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		//cout<<": Failed to CoInitialize() COM."<<endl;
		return -1;
	}
	try
	{
		//Connect to DB
		ADODB::_ConnectionPtr pConn("ADODB.Connection");//Create connection object PConn
		hr = pConn->Open(strConnect,"","",ADODB::adConnectUnspecified);//Connect to the database
		ADODB::_RecordsetPtr pRs("ADODB.Recordset");
		ADODB::_RecordsetPtr pRs1("ADODB.Recordset");
		int nRow;

		//QUERY :Get word code
		_bstr_t getquery = "SELECT MaTu From Tu WHERE CachViet = \"";
		getquery += word;
		getquery += "\" or CachViet = \"";
		getquery += refword;
		getquery += "\"";
		pRs->Open(getquery,
			_variant_t((IDispatch*)pConn,true),
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText
			);
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs->get_Fields(&pFields);
			int count = pFields->GetCount();
			nRow = 0;
			if(SUCCEEDED(hr)&& pFields && count > 0)//get success and have result
			{
				while (!pRs->AdoNSEOF)
				{
					for (long nIndex=0; nIndex < count; nIndex++)
					{
						if(nRow % 2 == 0)
							MaTu = _bstr_t(pFields->GetItem(nIndex)->GetValue());
						else
							MaTuLienQuan = _bstr_t(pFields->GetItem(nIndex)->GetValue());
					}
					pRs->MoveNext();
					nRow++;
				}
				if(nRow < 2)
				{
					pRs->Close();
					pConn->Close();
					return 0;//khong tim thay tu trong bang tu
				}
			}
			else
				return -2;//Query fail
		}
		else return -2;
		pRs->Close();
		//==================================================================================================
		//QUERY :test exist
		_bstr_t existquery = "SELECT MaTu From LienQuan WHERE (MaTu = \"";
		existquery += MaTu;
		existquery += "\" and MaTuLienQuan = \"";
		existquery += MaTuLienQuan;
		existquery += "\") or (MaTu = \"";
		existquery += MaTuLienQuan;
		existquery += "\" and MaTuLienQuan = \"";
		existquery += MaTu;
		existquery += "\")";
		hr = pRs1->Open(existquery, 
			_variant_t((IDispatch*)pConn,true), 
			ADODB::adOpenUnspecified,
			ADODB::adLockUnspecified, 
			ADODB::adCmdText);
		nRow = 0;
		if(SUCCEEDED(hr))
		{
			ADODB::Fields *pFields = NULL;//Init field to get info
			hr = pRs1->get_Fields(&pFields);
			int count = pFields->GetCount();
			if(SUCCEEDED(hr)&& pFields && pFields->GetCount() > 0)//get success and have result
			{
				while (!pRs1->AdoNSEOF)
				{
					nRow ++;
					pRs1->MoveNext();
				}
			}
			pRs1->Close();
			if(nRow != 0)
			{
				pConn ->Close();
				return 0;
			}
		}

		//===================================================================================================
		//QUERY : add to LienQuan table
		_bstr_t addquery = "INSERT INTO LienQuan(MaTu,MaTuLienQuan,LoaiLienQuan) VALUES(\"";
		addquery += MaTu ;
		addquery += "\",\"";
		addquery += MaTuLienQuan;
		addquery += "\",\"";
		addquery += type;
		addquery += "\")";

		hr = pConn->Execute(addquery,NULL,ADODB::adExecuteNoRecords);
		if(FAILED(hr))
		{
			pConn->Close();
			return -2;
		}
		pConn->Close();
	}
	catch(_com_error *e)
	{
		return -1;
	}
	CoUninitialize();
	return 1;
}
